// pages/apply/apply.js
const {request ,loginApi} = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    showArea: false,
    storeList: [],
    storeName: '',
    storeIndex: '',
    columns: [],
    name: '',
    phone: '',
    reason: '',
    area: '',
    p_id: '',
    a_id: '',
    c_id: '',
    addr: '',
    user: ''
  },

  showPoup(e) {
    this.setData({
      show: true
    })
  },
  
  onClose() {
    this.setData({ show: false, showArea: false });
  },
  
  close(e){
    this.setData({
      showArea:false,
      area:e.detail.value&&e.detail.value.join(","),
      p_id:e.detail.pid,
      c_id:e.detail.cid,
      a_id:e.detail.aid
    })
    this.getStore()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStore()
  },
  
  showArea() {
    this.setData({
      showArea: true
    })
  },
  getStore() {
    request('/store_lst_page', 'POST', {
      p_id: this.data.p_id,
      c_id: this.data.c_id,
      a_id: this.data.a_id
    }).then(res => {
      console.log(res)
      const columns = []
      if (res.data.code == 200) {
        res.data.data.forEach(item => {
          columns.push(item.title)
        })
        this.setData({
          columns: columns,
          storeList: res.data.data
        })
      }
    })
  },
  onConfirm(e) {
    this.setData({
      show: false,
      storeIndex: e.detail.index,
      storeName: e.detail.value
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  
  submit() {
  
    const submitData = {
      member_id: wx.getStorageSync('user').member_id,
      name:  this.data.name,
      phone:  this.data.phone,
      reason:  this.data.reason,
      s_id: this.data.storeName && this.data.storeList[this.data.storeIndex].id
    }
    console.log(this.data.storeList[this.data.storeIndex].id)
    if(!submitData.name){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.phone){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.s_id){
      wx.showToast({
        title: '请选择门店',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!submitData.reason){
      wx.showToast({
        title: '请输入您的需求',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.showLoading({
      title: '',
    })
    request('/sub_agent', 'POST', submitData).then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.code != 200) {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
        return;
      }
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userInfo = wx.getStorageSync('user')
    if (userInfo && userInfo.id) {
      this.setData({
        user: userInfo
      })
    }
    console.log(this.data.user)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})